package com.ybkj.daijia.server.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.config.RentApi;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.RentAppSetting;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Blazk.
 */
@RestController
@RequestMapping(value = "rentappsetting")
public class RentAppSettingController {

    @Autowired
    private RentApi rentApi;


    @Autowired
    private SettingUtils settingUtils;

    @RequestMapping(value = "rentappsetting", method = RequestMethod.GET)
    public ModelAndView rentappsetting() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentappsetting")) {
            ModelAndView mv = new ModelAndView();

            mv.setViewName("rentappsetting/rentappsetting");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }


    @RequestMapping(value = "updaterentappsetting", method = RequestMethod.POST)
    public Map<String, Object> updaterentappsetting(
        @RequestParam("id") Long id,
        @RequestParam("isRemote") Integer isRemote,
        @RequestParam("isDifferentShop") Integer isDifferentShop,
        @RequestParam("counterFee") Double counterFee,
        @RequestParam("basicPremium") Double basicPremium,
        @RequestParam("nightService") Double nightService,
        @RequestParam("gatherStartTime") Long gatherStartTime,
        @RequestParam("gatherEndTime") Long gatherEndTime,
        @RequestParam("serviceStartTime") Long serviceStartTime,
        @RequestParam("serviceEndTime") Long serviceEndTime,
        @RequestParam("childSafetyCost") Double childSafetyCost,
        @RequestParam("matchDriver") Double matchDriver,
        @RequestParam("offsiteReturnFee") Double offsiteReturnFee,
        @RequestParam("difShopReturnFee") Double difShopReturnFee,
        @RequestParam("dispatchCarFee") Double dispatchCarFee,
        @RequestParam("viewDay") Long viewDay,
        HttpServletRequest req, HttpServletResponse resp
    ) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));

        if (matchDriver != null) {
            kv.add("matchDriver", String.valueOf(matchDriver));
        }
        if (childSafetyCost != null) {
            kv.add("childSafetyCost", String.valueOf(childSafetyCost));
        }
        if (serviceEndTime != null) {
            kv.add("serviceEndTime", String.valueOf(serviceEndTime));
        }
        if (serviceStartTime != null) {
            kv.add("serviceStartTime", String.valueOf(serviceStartTime));
        }
        if (gatherEndTime != null) {
            kv.add("gatherEndTime", String.valueOf(gatherEndTime));
        }
        if (gatherStartTime != null) {
            kv.add("gatherStartTime", String.valueOf(gatherStartTime));
        }
        if (nightService != null) {
            kv.add("nightService", String.valueOf(nightService));
        }
        if (isRemote != null) {
            kv.add("isRemote", String.valueOf(isRemote));
        }
        if (isDifferentShop != null) {
            kv.add("isDifferentShop", String.valueOf(isDifferentShop));
        }
        if (counterFee != null) {
            kv.add("counterFee", String.valueOf(counterFee));
        }
        if (basicPremium != null) {
            kv.add("basicPremium", String.valueOf(basicPremium));
        }
        if (difShopReturnFee != null) {
            kv.add("difShopReturnFee", String.valueOf(difShopReturnFee));
        }
        if (offsiteReturnFee != null) {
            kv.add("offsiteReturnFee", String.valueOf(offsiteReturnFee));
        }
        if (dispatchCarFee != null) {
            kv.add("dispatchCarFee", String.valueOf(dispatchCarFee));
        }
        if (viewDay != null) {
            kv.add("viewDay", String.valueOf(viewDay));
        }

        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.UPDATE_RENT_APP_SETTING, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "selectRentAppSetting", method = RequestMethod.GET)
    public RentAppSetting selectRentAppSetting(
        HttpServletRequest req, HttpServletResponse resp
    ) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_RENT_APP_SETTING)
            .append("?appkey={appkey}");
        Map<String, String> kv = new HashMap<String, String>();
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());

        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject dataList = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().create();
                RentAppSetting rentAppSetting = gson
                    .fromJson(dataList.toString(), RentAppSetting.class);
                return rentAppSetting;
            }
        }
        return null;
    }


}
